package com.qfhd.soft.cjzcore.TaskCenter.CSJ;

import android.content.Context;
import android.view.View;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.qfhd.soft.cjzcore.Core.CJLogger;
import com.qfhd.soft.cjzcore.Listener.CJAdListener;
import com.qfhd.soft.cjzcore.Listener.CJRetListListener;
import com.qfhd.soft.cjzcore.TaskCenter.CJAdCenter;
import com.qfhd.soft.cjzcore.TaskCenter.TTAdManagerHolder;

import java.util.List;

public class CJCSJ_List_Navi {

    private TTAdNative mTTAdNative;
    private CJRetListListener retListListener;
    private CJAdListener adListener;

    private static CJCSJ_List_Navi csj_navi;

    public static CJCSJ_List_Navi getInstance() {
        if (csj_navi == null) {
            synchronized (CJCSJ_List_Navi.class) {
                csj_navi = new CJCSJ_List_Navi();
            }
        }
        return csj_navi;
    }

    public void getAds(Context context, int width, int height, CJRetListListener retListListener, CJAdListener adListener) {
        this.retListListener = retListListener;
        this.adListener = adListener;
        //feed广告请求类型参数
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(CJCSJsdk.Message)
                .setSupportDeepLink(true)
                .setExpressViewAcceptedSize(width, height) //期望模板广告view的size,单位dp
                .setAdCount(3) //请求广告数量为1到3条
                .setImageAcceptedSize(640,320) //这个参数设置即可，不影响个性化模板广告的size
                .build();
        mTTAdNative = TTAdManagerHolder.get().createAdNative(context.getApplicationContext());
        mTTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {
                CJLogger.v("CSJ List Navi onError code:" + code + " mas:" + message);
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                CJLogger.v("CJCSJ_List_Navi size:" + ads.size());
                if (ads == null || ads.isEmpty()) {
                    return;
                }

                if (retListListener != null) {
                    retListListener.returnList(ads);
                }

                for (TTNativeExpressAd ad: ads) {
                    ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
                        @Override
                        public void onAdClicked(View view, int type) {
//                            TToast.show(NativeExpressListActivity.this, "广告被点击");
                            if(adListener != null){
                                adListener.onStatus(CJAdCenter.AD_Status_Click);
                            }
                        }

                        @Override
                        public void onAdShow(View view, int type) {
//                            TToast.show(NativeExpressListActivity.this, "广告展示");
                        }

                        @Override
                        public void onRenderFail(View view, String msg, int code) {
//                            TToast.show(NativeExpressListActivity.this, msg + " code:" + code);
                            CJLogger.v("onRenderFail code:" + code + " msg:" + msg);
                        }

                        @Override
                        public void onRenderSuccess(View view, float width, float height) {
                            //返回view的宽高 单位 dp
//                            TToast.show(NativeExpressListActivity.this, "渲染成功");
//                            myAdapter.notifyDataSetChanged();
                            CJLogger.v("CJCSJ_List_Navi onRenderSuccess");
                        }
                    });
                    ad.render();
                }
            }
        });
    }
}
